package com.mujin.keji.collectionsystem.module.tripplan.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.search.route.MassTransitRouteLine;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.listeners.CustomItemClickListener;
import com.mujin.keji.collectionsystem.module.tripplan.dto.MassTransitRouteLineDto;
import com.mujin.keji.collectionsystem.utils.DateUtil;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 公交路线规划列表适配器
 *
 * @author weishixiong
 * @Time 2018-04-10
 */

public class CommonBusAdapter extends RecyclerView.Adapter<CommonBusAdapter.ViewHolder> {
    private Activity mContext;
    private List<MassTransitRouteLineDto> datas;
    private CustomItemClickListener listener;


    public CommonBusAdapter(Activity context, List<MassTransitRouteLineDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_comm_bus, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvTime.setText(DateUtil.minForDay(datas.get(position).getMassTransitRouteLine().getDuration() / 60));
        int distance = 0;
        //获取步行的距离
        //遍历每条路线的步骤
        for (int j = 0; j < datas.get(position).getMassTransitRouteLine().getNewSteps().size(); j++) {
            //步骤
            List<MassTransitRouteLine.TransitStep> transitStepList = datas.get(position).getMassTransitRouteLine().getNewSteps().get(j);
            for (int k = 0; k < transitStepList.size(); k++) {
                String instructions = transitStepList.get(k).getInstructions();
                //
                if (instructions.contains("步行") && instructions.contains("米")) {
                    //提取文字中的数字
                    String regEx = "[^0-9]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher(instructions);
                    distance += +Integer.parseInt(m.replaceAll("").trim());
                    holder.tvWalkDistance.setText("步行" + distance + "米");
                }
                if (EmptyUtils.isNotEmpty(transitStepList.get(k).getBusInfo())) {
                    holder.tvBusLine.setText(holder.tvBusLine.getText().toString() + "/" + transitStepList.get(k).getBusInfo().getName());
                    if (TextUtils.isEmpty(holder.tvStartBus.getText().toString())) {
                        holder.tvStartBus.setText(transitStepList.get(k).getBusInfo().getDepartureStation() + "上车");
                    }
                }

            }


        }
        if (datas.get(position).isMinTime()) {
            holder.tvMark.setText("时间短");
            holder.tvMark.setVisibility(View.VISIBLE);
        }
        else if (datas.get(position).isMinWakl()) {
            holder.tvMark.setText("少步行");
            holder.tvMark.setVisibility(View.VISIBLE);
        }
        else{
            holder.tvMark.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvMark;
        private TextView tvWalkDistance;
        private TextView tvTime;
        private TextView tvBusLine;
        private TextView tvStartBus;

        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            tvMark = (TextView) itemView.findViewById(R.id.tv_mark);
            tvWalkDistance = (TextView) itemView.findViewById(R.id.tv_walk_distance);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvBusLine = (TextView) itemView.findViewById(R.id.tv_bus_line);
            tvStartBus = (TextView) itemView.findViewById(R.id.tv_start_bus);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });
        }

    }
}
